﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using log4net;
using System.Web.Routing;
using System.Text.RegularExpressions;

namespace Windvoice.Framework
{
    public class WindvoiceBasicAttribute : ActionFilterAttribute
    {
        private ILog VisitLogger
        {
            get { return LogManager.GetLogger("visitlogger"); }
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var request = filterContext.HttpContext.Request;
            var useragent = request.UserAgent.ToLower();

            var match = Regex.Match(useragent, @"MSIE (6[\.0-9]{0,})");

            if (useragent.ToLower().Contains("msie") && !useragent.ToLower().Contains("opera") && match.Groups.Count > 0 && match.Groups[0].Success)
                filterContext.Result = new ViewResult() { ViewName = "_ie6" };

            string log = String.Format("{0}\t{1}\t{2}\t{3}\t{4}", request.UserHostAddress, request.RawUrl, request.HttpMethod, request.UserAgent, request.UrlReferrer==null||String.IsNullOrEmpty(request.UrlReferrer.AbsoluteUri) ? "null" : request.UrlReferrer.AbsoluteUri);
            VisitLogger.Info(log);
        }

        private string GetRouteDictionaryValues(RouteValueDictionary dictionary)
        {
            var result = String.Empty;
            foreach (var k in dictionary.Keys)
                result += String.Format("{0}:{1};", k, dictionary[k]);
            return result;
        }
    }

    
}
